package com.ygxsk.carhome.retrofit;


import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ObjectUtils;
import com.ygxsk.carhome.constant.ConstantSign;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Converter;

public class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {

    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");

    @Override
    public RequestBody convert(T value) throws IOException {

        try {
            return RequestBody.create(JSON.toJSONBytes(value), MEDIA_TYPE);
        } catch (Exception e) {

            if (!ObjectUtils.isEmpty(e)) {
//                LogUtils.d(ConstantApi.LOGCAT_NOMAL + "json-convert-请求异常->" + e.getLocalizedMessage());

                //2022-11-24 15:50:52这里直接添加日志信息
                new ApiRequestSubListener<T>() {
                }.addLogInApiRequestListener(null,//
                        ApiService.addLogUrl,//
                        ConstantSign.REQUEST_METHOD_POST,//
                        null,//
                        e.getLocalizedMessage().toString());//

            } else {

                new ApiRequestSubListener<T>() {
                }//
                        .addLogInApiRequestListener(null,//
                                ApiService.addLogUrl,//
                                ConstantSign.REQUEST_METHOD_POST,//
                                null,//
                                "FastJsonRequestBodyConverter转换请求异常");//

            }

            throw new IOException("Could not write JSON: " + e.getMessage(), e);
        }

    }
}
